package 二叉树相关问题.中度题;

public class 二叉树的最长交错路径 {

    int max=0;
    public int longestZigZag(TreeNode root) {
        dfs(root,true,0);
        dfs(root,false,0);
        return max;
    }

    private void dfs(TreeNode root, boolean b, int i) {
        if (root==null)
            return;
        max=Math.max(max,i);
        if (b){
            dfs(root.left,!b,i+1);
            dfs(root.right,b,1);
        }
        else {
            dfs(root.right,!b,i+1);
            dfs(root.left,b,1);
        }
    }
}
